package com.soundShape.mediators.administration.customer
{
	
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.mediators.AbstractViewMediator;
	import com.soundShape.popUps.EditCustomer;
	import com.soundShape.popUps.EditEmployee;
	import com.soundShape.views.administration.Administration;
	import com.soundShape.views.administration.customer.Customers;
	import com.soundShape.vo.CustomerVO;
	import com.soundShape.vo.EmployeeVO;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.ListEvent;
	import mx.managers.PopUpManager;
	
	import nz.co.codec.flexorm.criteria.Criteria;
	
	
	
	
	public class CustomerMediator extends AbstractViewMediator
	{
		
		
		
		
		public function CustomerMediator()
		{
			super(Customers);
		}
		
		/**
		 * Since the AbstractViewMediator sets the view via Autowiring in Swiz,
		 * we need to create a local getter to access the underlying, expected view
		 * class type.
		 */
		public function get view():Customers
		{
			return this._view as Customers;
		}
		
		/**
		 * The <code>init()</code> method is fired off automatically by the 
		 * AbstractViewMediator when the creation complete event fires for the
		 * corresponding ViewMediator's view. This allows us to listen for events
		 * and set data bindings on the view with the confidence that our view
		 * and all of it's child views have been created and live on the stage.
		 */
		override protected function init():void
		{
			super.init();
			
			this.setViews();
			
		}
		
		override protected function setViewDataBindings():void
		{
			super.setViewDataBindings();
			
			
		}	
		
		override protected function setViewListeners():void
		{
			super.setViewListeners();
			
			//this.view.btnBack.addEventListener(MouseEvent.CLICK, handleBtnBack);
			
			this.view.dgCustomers.addEventListener(ListEvent.ITEM_CLICK, handleItemClick)
			
			this.view.dgCustomers.addEventListener(SoundShapeEvent.SPECIAL_DATE, handleItemClickTest);
			
		}
		
		private function setViews():void
		{
			var criteria:Criteria = applicationModel.entityManager.createCriteria(CustomerVO);
			
			var customers:ArrayCollection = this.applicationModel.entityManager.fetchCriteria(criteria);
			
			this.view.dgCustomers.dataProvider = customers;
		}
		
		
		/**
		 * *******************************************************************************
		 * Handlers
		 * *******************************************************************************
		 */
		private function handleBtnBack(mouseEvent:MouseEvent):void
		{
			this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.SWITCH_TO_ADMIN_VIEW));
		}
		
		private function handleBtnNewEmployee(mouseEvent:MouseEvent):void
		{
			this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.SWITCH_TO_NEW_EMPLOYEE_VIEW));
		}
		
		private function handleItemClick(listEvent:ListEvent):void
		{
			var customerVO:CustomerVO = ArrayCollection(this.view.dgCustomers.dataProvider).getItemAt(listEvent.rowIndex) as CustomerVO;
			var editCustomer:EditCustomer = new EditCustomer();
			editCustomer.customerVO = customerVO;
			editCustomer.employeeVO = this.applicationModel.employeeVO;
			editCustomer.entityManager = this.applicationModel.entityManager;
			editCustomer.callback = callback;
			PopUpManager.addPopUp(editCustomer, this.view, true);
			PopUpManager.centerPopUp(editCustomer);
		}
		
		private function handleItemClickTest(soundShapeEvent:SoundShapeEvent):void
		{
			Alert.show("foo", "foo");
		}
		
		private function callback():void
		{
			this.setViews();
		}
		
		
		/**
		 * *******************************************************************************
		 * Swiz EventHanders
		 * *******************************************************************************
		 */
		
		
		
		
		
		
		
	}
	
}
